package org.example.onlinejudge.config;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 自定义登出成功处理器
 * 负责处理用户登出成功后的操作
 * 实现逻辑：
 * 1. 清除用户的session，解决登出后重定向到登录页面的session问题
 * 2. 重定向到登录页面，并附加logout=true参数，提示用户已成功登出
 */
@Component
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, 
                               HttpServletResponse response,
                               Authentication authentication) throws IOException, ServletException {
        
        // 清除session
        if (request.getSession(false) != null) {
            request.getSession().invalidate();
        }
        
        // 重定向到登录页面
        response.sendRedirect("/login?logout=true");
    }
}


